package com.sinothk.cloud.mq.websocket.controller;

import com.sinothk.cloud.mq.websocket.bean.IMBean;
import com.sinothk.cloud.mq.websocket.service.WebSocketIMService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@Api(tags = "WebSocketIM模块")
@RequestMapping("/wsIM")
@RestController
public class WebSocketIMController {

    private final WebSocketIMService webSocketIMService;

    public WebSocketIMController(WebSocketIMService webSocketIMService) {
        this.webSocketIMService = webSocketIMService;
    }

    @ApiOperation(value = "获取所有在线用户")
    @ApiImplicitParam(name = "userId", value = "当前登录人userId", required = true)
    @GetMapping("/getOnlineUsers")
    public String getOnlineUsers(@RequestParam(value = "userId") String userId) {
        return "所有在线用户";
    }

    @ApiOperation(value = "发给指定用户", notes = "提供给非IM用户或未登录用户发消息给登录用户")
    @PostMapping("/sendMsg")
    public String sendMsg(@RequestBody IMBean iMBean) {
//        if (fromId == null || fromId.length() == 0) {
//            // 来自 后台 -> 客户端(PC/App)
//        } else {
//            // 来自 客户端(PC/App) -> 客户端
//        }
        try {
            webSocketIMService.sendMsg(iMBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "发给指定用户";
    }

    @ApiOperation(value = "发给指定多人用户", notes = "提供给非IM用户或未登录用户发消息给登录用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fromId", value = "发送人userId"),
            @ApiImplicitParam(name = "toIds", value = "接收人userId集合", required = true),
            @ApiImplicitParam(name = "content", value = "content", required = true)})
    @PostMapping("/sendMsgToMany")
    public String sendMsgToMany(@RequestBody IMBean iMBean) {
//        if (fromId == null || fromId.length() == 0) {
//            // 来自 后台 -> 客户端(PC/App)
//        } else {
//            // 来自 客户端(PC/App) -> 客户端
//        }
        try {
            webSocketIMService.sendMsgToMany(iMBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "发给指定多人用户";
    }
}
